package com.we823.cms.repository.service.cms;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import com.we823.cms.common.web.pageable.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.we823.cms.model.cms.Channel;
import com.we823.cms.repository.dao.cms.ChannelMapper;
import com.we823.cms.repository.service.AbstractBaseService;
import tk.mybatis.mapper.entity.Example;

@Service("channelService")
@Scope("prototype")
public class ChannelService extends AbstractBaseService<Channel, Integer>{

	private ChannelMapper channelMapper;
	
	@Autowired
	public ChannelService(ChannelMapper categoryMapper) {
		super(categoryMapper);
		this.channelMapper = categoryMapper;
	}

	public List<Channel> findByType(int type, Sort sort) {
		Channel channel = new Channel();
		channel.setType(type);
		setSortInfo(sort);
		return channelMapper.select(channel);
	}
	
	public PageInfo<Channel> findByType(int type,PageRequest pageRequest){
		Example example = new Example(Channel.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andCondition("type=", type);
        setPageInfo(pageRequest);
		return new PageInfo<>(channelMapper.selectByExample(example));
	}

	public void updateOrderidById(Integer id, Integer orderid) {
		Channel channel = new Channel();
		channel.setId(id);
		channel.setOrderid(orderid);
		channelMapper.updateByPrimaryKeySelective(channel);
	}

	public PageInfo<Channel> findByParent(Channel parent, PageRequest pageRequest) {
		setPageInfo(pageRequest);
		return new PageInfo<>(this.findByParent(parent));
	}

	public List<Channel> findByParent(Channel parent, Sort sort) {
		setSortInfo(sort);
		return this.findByParent(parent);
	}

	public List<Channel> findByParent(Channel parent) {
		if (parent!=null){
			return this.findByParentId(parent.getId());
		}
		return null;
	}

	public List<Channel> findByParentId(Integer parentId){
		Example example = new Example(Channel.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("parentId", parentId);
		return channelMapper.selectByExample(example);
	}

}
